package com.gg.communication.ui.ctr;

import com.gg.communication.communication.BaseCommunication;
import com.gg.communication.communication.tcp.TcpClient;
import com.gg.communication.communication.udp.client.UdpClient;
import com.gg.communication.event.ReceiveListener;
import com.gg.communication.event.check.BoardSend485Check;
import com.gg.communication.model.MessageType;
import com.gg.communication.util.ByteUtil;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import org.controlsfx.control.ToggleSwitch;

import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutionException;

/**
 * @author sakura
 * @date 2025/6/3
 */
public class ClientCtr {
    public TextField ip;
    public TextField port;
    public Button connect;
    public TextArea sendText;
    public Button send;
    public TextArea receiveText;
    public CheckBox isHex;
    public CheckBox revHex;
    public TextField timeout;
    public Button send2;
    public Button testSendType;
    public TextField waitTimeout;
    public ToggleSwitch cement;

    public void connect(ActionEvent actionEvent) throws SocketException, UnknownHostException {
        //client = new UdpClient(ip.getText(), Integer.parseInt(port.getText()));
        //client.setTimeOut(Integer.parseInt(timeout.getText()));
        //client.setResponseTimeout(Integer.parseInt(waitTimeout.getText()));
        //CheckDecorator check = new BoardSend485Check(null);
        //client.setReveCheck(check);
        //client.connect();
    }

    public void sendMsg(ActionEvent actionEvent) throws ExecutionException, InterruptedException {
        new Thread(() -> {
            byte[] res = send(1 + ":" + sendText.getText(), MessageType.TYPE1);
            if (revHex.isSelected()) {
                receiveText.appendText(ByteUtil.byteArrayToHexString(res) + "\n");
            } else {
                receiveText.appendText(new String(res) + "\n");
            }
        }).start();
    }

    public void sendMsg2(ActionEvent actionEvent) {
        new Thread(() -> {
            byte[] res = send(2 + ":" + sendText.getText(), MessageType.TYPE2);
            if (revHex.isSelected()) {
                receiveText.appendText(ByteUtil.byteArrayToHexString(res) + "\n");
            } else {
                receiveText.appendText(new String(res) + "\n");
            }

        }).start();
    }

    private byte[] send(String data, MessageType type) {


        byte[] msg = data.getBytes();
        byte[] res = null;
        if (isHex.isSelected()) {
            msg = ByteUtil.hexStringToByteArray(data);
        }
        try {
            BaseCommunication client = null;
            if (cement.isSelected()) {
                client = TcpClient.initInstance(ip.getText(), Integer.parseInt(port.getText()));
            } else {
                client = UdpClient.initInstance(ip.getText(), Integer.parseInt(port.getText()));
            }
            client.setReveCheck(new BoardSend485Check(null));
            client.setReceiveListener(new ReceiveListener() {
                @Override
                public void receive(byte[] msg, BaseCommunication communication) {
                    System.out.println("<==");
                    ByteUtil.byteArrayToHexString(msg);
                }

                @Override
                public void err(Exception e) {

                }
            });
            //client.setHost(ip.getText());
            //client.setPort(Integer.parseInt(port.getText()));
            //(ip.getText(), Integer.parseInt(port.getText()));

            client.start();


            //try {
            if (type == MessageType.TYPE1) {
                res = client.send(msg);
            } else if (type == MessageType.TYPE2) {
                res = client.send2(msg);
            }
        } catch (ExecutionException e) {
            res = ("发送失败2" + data + e.getMessage() + "\n").getBytes();
            e.printStackTrace();
        } catch (InterruptedException e) {
            res = ("发送失败" + e.getMessage() + "\n").getBytes();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return res;
    }

    public void testSendType(ActionEvent actionEvent) {
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                int finalI = i;
                new Thread(() ->
                        send("send 1:" + finalI + ":" + sendText.getText(), MessageType.TYPE1)).start();
            }
            send("send 2:" + sendText.getText(), MessageType.TYPE2);

        }).start();
    }
}
